package com.ayuku.retail.business.category;

import java.util.List;

import com.ayuku.commons.AyukuDB;
import com.ayuku.retail.persistence.model.Category;

public class CategoryImpl {
	public static List<Category> getList(String arg) throws Exception {
		List<Category> lst = AyukuDB
				.getPersist()
				.readList(
						Category.class,
						"SELECT * FROM Category WHERE UCASE(code) LIKE ucase(?) or ucase(NAME) LIKE ucase(?) LIMIT 1000",
						new Object[] { arg, arg });
		return lst;
	}

	public static List<Category> getListForLookup(String arg) throws Exception {
		List<Category> lst = AyukuDB
				.getPersist()
				.readList(
						Category.class,
						"SELECT * FROM Category WHERE UCASE(code) LIKE ucase(?) or ucase(NAME) LIKE ucase(?) LIMIT 200",
						new Object[] { arg, arg });
		return lst;
	}

	public static Category getById(String key) throws Exception {
		Category obj = (Category) AyukuDB.getPersist().readByPrimaryKey(
				Category.class, new Object[] { key });
		return obj;
	}

	public static int add(Category obj) throws Exception {
		return AyukuDB.getPersist().insert(obj);
	}

	public static int update(Category obj) throws Exception {
		return AyukuDB.getPersist().update(obj);
	}

	public static int delete(String key) throws Exception {
		int n = AyukuDB.getPersist().executeUpdate(
				"delete from Category where code = ?",
				new Object[] { key.trim() });
		return n;
	}

	public static int delete(List<String> ids) throws Exception {
		int n = 0;
		try {
			AyukuDB.getPersist().setAutoCommit(false);
			for (int j = 0; j < ids.size(); j++) {
				String key = ids.get(j);
				n += delete(key);
			}
			AyukuDB.getPersist().commit();
		} catch (Exception e) {
			e.printStackTrace();
			AyukuDB.getPersist().rollback();
			throw new Exception();
		} finally {
			AyukuDB.getPersist().setAutoCommit(true);
		}
		return n;
	}

}
